package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ParameterTypeMapper;
import com.zhentao.pojo.ParameterType;
import com.zhentao.pojo.ProductParameter;
import com.zhentao.service.ProductParameterService;
import com.zhentao.mapper.ProductParameterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author lenovo
* @description 针对表【product_parameter(商品参数值表)】的数据库操作Service实现
* @createDate 2025-08-29 09:27:34
*/
@Service
public class ProductParameterServiceImpl extends ServiceImpl<ProductParameterMapper, ProductParameter>
    implements ProductParameterService{

    @Autowired
    private ParameterTypeMapper parameterTypeMapper;
    @Override
    public List<ProductParameter> getProductParam(Integer productId) {

        List<ProductParameter> productParameters = list(new QueryWrapper<ProductParameter>().eq("product_id", productId));
        if (!productParameters.isEmpty()) {
            List<Long> typeIds = productParameters.stream()
                    .map(ProductParameter::getTypeId)
                    .distinct()
                    .toList();

            List<ParameterType> parameterTypes = parameterTypeMapper.selectBatchIds(typeIds);

            Map<Long, String> typeNameMap = parameterTypes.stream()
                    .collect(Collectors.toMap(ParameterType::getTypeId, ParameterType::getName));

            for (ProductParameter productParameter : productParameters) {
                String typeName = typeNameMap.get(productParameter.getTypeId());
                if (typeName != null) {
                    productParameter.setDescription(typeName);
                }
            }
        }
        return productParameters;

    }
}




